package com.login.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;



public class DbUtils {
	//三大参数
	private static final String URL="jdbc:mysql://localhost:3306/usersys";
	private static final String USER="root";
	private static final String PASSWORD="123";
	//jdbc��ض���
	protected static Statement s=null;
	protected static ResultSet rs=null;
	protected static Connection conn=null;
	
	//��������
	public static synchronized Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (Exception e) {			
			e.printStackTrace();
		} 
		
		return conn;	
	}
	//���Է���
	@Test
	public void test(){
		getConnection();
	}
	//ִ��insert update delete ���
	public static int executeUpdate(String sql){
		int result=0;
		try {
			s=getConnection().createStatement();
			result=s.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return result;		
	}
	//select return result�����
	public static ResultSet executeQuery(String sql){
		try {
			s=getConnection().createStatement();
			rs=s.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	//ִ��sql���  ������PrepareStatement�����
	public static PreparedStatement executePrepareStetement(String sql){
		PreparedStatement ps=null;
		try {
			ps=getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ps;
	}
	//����ع�
	public static void roolback(){
		try {
			getConnection().rollback();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//�ر����ݿ����Ӷ���
	public static void close(){
		try {
			if(rs!=null){
				rs.close();
			}if(s!=null){
				s.close();
			}if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
}
